using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class miniweb_MasterPage : System.Web.UI.MasterPage
{
    #region Variable

    protected long Id
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["id"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["id"]);
        }
    }
    public long memid
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["memid"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["memid"]);
        }
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
       
        LoadData();
        LoadMenuProduct();

    }
    #region LoadData
    private void LoadData()
    {
        ADESSA.Entities.Company _Company = new ADESSA.Entities.Company();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {

                Session["CompanyID"] = _Company.CompanyID;
                Country _Country = CountryService.GetCountry(_Company.Country);
                ltrNameCompany.Text = _Company.CompanyName;
                ltrNameCompanyBanner.Text = _Company.CompanyName;
                if (_Country != null)
                {
                    ltrCountry.Text = "[ " + _Country.Name + "]";
                }

                //Load CatPro
                ProductCat _ProductCatSubSub = ProductCatService.GetProductCat(_Company.ProCateID);
                if (_ProductCatSubSub != null)
                {
                    ProductCat _ProductCatSub = ProductCatService.GetProductCat(_ProductCatSubSub.Parrentid);
                    if (_ProductCatSub != null)
                    {

                    }
                }
                //Load Member
                Member _Member = MemberService.GetMember(_Company.MemberID);
                if (_Member != null)
                {
                    if (_Member.GoldMember == true)
                    {
                        MembershipFee _MembershipFee = MembershipFeeService.GetMembershipFee(_Member.MembershipFeeID);
                        if (_MembershipFee != null)
                        {
                            ltrStatusMem.Text = "<strong>Gold Member</strong>" + _MembershipFee.Title;
                        }

                    }
                    else
                    {
                        ltrStatusMem.Text = "<strong>Free Member</strong>";
                    }
                }




            }
        }
        else if (Session["CompanyID"] != null)
        {



            _Company = CompanyService.GetCompany(long.Parse(Session["CompanyID"].ToString()));
            if (_Company != null)
            {
                Country _Country = CountryService.GetCountry(_Company.Country);
                ltrNameCompany.Text = _Company.CompanyName;
                ltrNameCompanyBanner.Text = _Company.CompanyName;
                Session["CompanyID"] = _Company.CompanyID;
                if (_Country != null)
                    ltrCountry.Text = "[ " + _Country.Name + "]";
            }

        }
    }
    #endregion
    #region Load Product New
    ArrayList CatproductList = new ArrayList();
    private void LoadMenuProduct()
    {
        if (Session["MemberID"] != null)
        {
            long idMem = long.Parse(Session["MemberID"].ToString());

            Criteria cri = new Criteria();

            cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, idMem, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.Enable, 1, Condition.EQUAL, Condition.AND);
            ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
            if (_ProductSellCollection.Count > 0)
            {
                ltrNumProduct.Text = _ProductSellCollection.Count.ToString();
                string join = string.Empty;
                foreach (ProductSell _ProductSell in _ProductSellCollection)
                {
                    join += _ProductSell.CatId + ",";
                }
                Criteria cri_ = new Criteria();

                cri_.add(ProductCat._Table + "." + ProductCatColumns.Enable, 1, Condition.EQUAL, Condition.AND);
                string query = cri_.Criter + " And ProCateID IN(" + join.TrimEnd(',') + ")";
                ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam(ProductCatColumns.SortOrder, OrderDirection.ASC, query);
                if (_ProductCatCollection.Count > 0)
                {
                    rptMenuCatProduct.DataSource = _ProductCatCollection;
                    rptMenuCatProduct.DataBind();
                }
            }






        }
    }




    #endregion
    protected void rptMenuCatProduct_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ProductCat _ProductCat = (ProductCat)e.Item.DataItem;
        Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");
        HtmlAnchor ahrTitle = (HtmlAnchor)e.Item.FindControl("ahrTitle");
        long idMem = long.Parse(Session["MemberID"].ToString());

        ahrTitle.HRef = string.Format("product.aspx?cid={0}", _ProductCat.ProCateID.ToString());
        Criteria cri = new Criteria();
        cri.add(ProductSell._Table + "." + ProductSellColumns.CatId, _ProductCat.ProCateID, Condition.EQUAL, Condition.AND);
        cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, idMem, Condition.EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
        ltrTitle.Text = _ProductCat.Name + " (" + _ProductSellCollection.Count.ToString() + ")";
    }
}
